package cn.stylefeng.guns.modular.basic.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.WarehouseStore;
import cn.stylefeng.guns.modular.basic.mapper.WarehouseStoreMapper;
import cn.stylefeng.guns.modular.basic.model.params.WarehouseStoreParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicPutgoodsResult;
import cn.stylefeng.guns.modular.basic.model.result.WarehouseStoreResult;
import  cn.stylefeng.guns.modular.basic.service.WarehouseStoreService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 仓库门店关系 服务实现类
 * </p>
 *
 * @author wp
 * @since 2025-08-26
 */
@Service
public class WarehouseStoreServiceImpl extends ServiceImpl<WarehouseStoreMapper, WarehouseStore> implements WarehouseStoreService {

    @Override
    public void add(WarehouseStoreParam param){
        WarehouseStore entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(WarehouseStoreParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(WarehouseStoreParam param){
        WarehouseStore oldEntity = getOldEntity(param);
        WarehouseStore newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public WarehouseStoreResult findBySpec(WarehouseStoreParam param){
        return null;
    }

    @Override
    public List<WarehouseStoreResult> findListBySpec(WarehouseStoreParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(WarehouseStoreParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<BasicPutgoodsResult> findByStoreIdAndWarehouseId(String storeId, String warehouseId) {
        return this.baseMapper.findByStoreIdAndWarehouseId(storeId, warehouseId);
    }

    private Serializable getKey(WarehouseStoreParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private WarehouseStore getOldEntity(WarehouseStoreParam param) {
        return this.getById(getKey(param));
    }

    private WarehouseStore getEntity(WarehouseStoreParam param) {
        WarehouseStore entity = new WarehouseStore();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
